package com.bugsnag.ohos;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

/**
 * IOUtils
 *
 * @since 2021-07-27
 */
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
class IoUtils {
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
    private static final int EOF = -1;

    private IoUtils() {
    }

    static void closeQuietly(final Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (final IOException ioe) {
            ioe.getMessage();
        }
    }

    static int copy(final Reader input, final Writer output) throws IOException {
        char[] buffer = new char[DEFAULT_BUFFER_SIZE];
        int count = 0;
        int read;
        while ((read = input.read(buffer)) != EOF) {
            output.write(buffer, 0, read);
            count += read;
        }
        if (count > Integer.MAX_VALUE) {
            return -1;
        }
        return count;
    }

    static void deleteFile(File file, Logger logger) {
        try {
            boolean hasDel = file.delete();
            logger.w("delete file  : " + hasDel, null);
        } catch (RuntimeException ex) {
            logger.w("Failed to delete file", ex);
        }
    }
}
